package com.pureut.order.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.order.domain.PullDown;
import com.pureut.order.export.OrderSupplierManagementimportExport;
import com.pureut.order.util.ExcelUtil;
import com.pureut.order.dto.OrderSupplierManagementDto;
import com.pureut.order.export.OrderSupplierManagementExport;
import com.pureut.order.service.OrderSupplierManagementService;
import com.pureut.order.vo.OrderReviewVo;
import com.pureut.order.vo.OrderSupplierManagementVo;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.net.URLEncoder;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 * 供应商管理模块
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/supplier-management")
public class OrderSupplierManagementController extends BaseController {


    private final OrderSupplierManagementService orderSupplierManagementService;

    /**
     * 供应商管理列表
     */
    //@SaCheckPermission("order:purchase:supplier:list")
    @GetMapping("/list")
    public TableDataInfo<OrderSupplierManagementDto> list(OrderSupplierManagementVo orderSupplierManagementVo, PageQuery pageQuery) {
        //查询供应商列表
        return orderSupplierManagementService.getList(orderSupplierManagementVo, pageQuery);
    }

    /**
     * 供应商新增
     */
    //@SaCheckPermission("order:purchase:supplier:add")
    @Log(title = "供应商新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody OrderSupplierManagementVo orderSupplierManagementVo) throws Exception {
        return toAjax(orderSupplierManagementService.addSupplier(orderSupplierManagementVo));
    }

    /**
     * 供应商修改
     */
    @Log(title = "供应商修改", businessType = BusinessType.UPDATE)
    //@SaCheckPermission("order:purchase:supplier:edit")
    @PutMapping("/update")
    public R<Void> update(@Validated @RequestBody OrderSupplierManagementVo orderSupplierManagementVo) throws Exception {
        return toAjax(orderSupplierManagementService.updateSupplierById(orderSupplierManagementVo));
    }

    /**
     * 供应商删除
     */
    //@SaCheckPermission("order:purchase:supplier:remove")
    @Log(title = "供应商删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public R<Void> delete(@PathVariable("ids") String ids) throws Exception {
        return toAjax(orderSupplierManagementService.deleteSupplierByIds(ids));
    }

    /**
     * 查看供应商信息
     */
    @GetMapping("/view/{id}")
    //@SaCheckPermission("order:purchase:supplier:query")
    public R<OrderSupplierManagementDto> view(@PathVariable("id") Long id) {
        return R.ok(orderSupplierManagementService.getSupplierById(id));
    }

    /**
     * 根据供应商编码查询供应商详情信息
     */
    @GetMapping("/view/getSupplierNum/{supplierNum}")
    //@SaCheckPermission("order:purchase:supplier:query")
    public R<OrderSupplierManagementDto> view(@PathVariable("supplierNum") String supplierNum) {
        return R.ok(orderSupplierManagementService.getSupplierByNum(supplierNum));
    }


    /**
     * 修改供应商开启状态
     */
    //@SaCheckPermission("order:purchase:supplier:updateStatus")
    @PutMapping("/updateStatus")
    public R<Void> updateStatus(@RequestBody OrderSupplierManagementVo orderSupplierManagementVo) {
        return toAjax(orderSupplierManagementService.updateStatus(orderSupplierManagementVo));
    }

    /**
     * 获取启用的供应商列表
     */
    @GetMapping("/getOpenSupplier")
    public R<List<HashMap<String,String>>> getOpenSupplier() {
        return R.ok(orderSupplierManagementService.getOpenSupplier());
    }

    /**
     * 获取启用的供应商列表(财务模块调用)
     */
    @GetMapping("/getOpenSupplierFinance")
    public R<Map<String,String>> getOpenSupplierFinance() {
        return R.ok(orderSupplierManagementService.getOpenSupplierFinance());
    }

    /**
     * 查看供应商信息(根据编码)
     */
    @GetMapping("/getSupplierByCode")
    public R<OrderSupplierManagementDto> getSupplierByCode(String code) {
        return R.ok(orderSupplierManagementService.getSupplierByCode(code));
    }
//    /**
//     * 冻结供应商
//     */
//    //@SaCheckPermission("order:purchase:supplier:frozen")
//    @Log(title = "冻结供应商", businessType = BusinessType.UPDATE)
//    @GetMapping("/frozen/{id}")
//    public R<Void> frozenSupplier(@PathVariable("id") Long id) {
//        return toAjax(orderSupplierManagementService.frozenSupplier(id));
//    }
//
//    /**
//     * 解冻供应商
//     */
//    //@SaCheckPermission("order:purchase:supplier:thaw")
//    @Log(title = "解冻供应商", businessType = BusinessType.UPDATE)
//    @GetMapping("/thaw/{id}")
//    public R<Void> thawSupplier(@PathVariable("id") Long id) {
//        return toAjax(orderSupplierManagementService.thawSupplier(id));
//    }
//
//    /**
//     * 撤销审核
//     */
//    //@SaCheckPermission("order:purchase:supplier:revoke")
//    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
//    @GetMapping("/revoke/{id}")
//    public R<Void> revokeApproval(@PathVariable("id") Long id) {
//        return toAjax(orderSupplierManagementService.revokeApproval(id));
//    }

//    /**
//     * 生成供应商导入模板
//     */
//    @PostMapping("/supplierTemplate")
//    public AjaxResult supplierTemplate(HttpServletResponse response) throws IOException {
//        ExcelUtil<OrderSupplierManagementimportExport> excelUtil = new ExcelUtil<OrderSupplierManagementimportExport>(OrderSupplierManagementimportExport.class);
//        excelUtil.exportTemplate(response, null, "供应商信息.xlsx");
//        return AjaxResult.success();
//    }

    /**
     * 生成供应商导入模板
     */
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        String fileName = "C:/Users/jack/Desktop/导入导出模板/导入模板/导入供应商.xlsx";
        File file = new File(fileName);
        ByteArrayOutputStream baos = null;
        ServletOutputStream out = null;
        try {

            InputStream inStream = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("导入供应商.xlsx", "UTF-8"));
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.flush();
            baos.close();
            out.flush();
            out.close();
        }
    }

    /**
     * 供应商信息导出
     */
    //@SaCheckPermission("order:purchase:supplier:export")
    @PostMapping("/supplierMessageExport")
    public R mouldMessageExport(HttpServletResponse response, OrderSupplierManagementVo orderSupplierManagementVo) throws IOException {
        List<OrderSupplierManagementExport> infoExport = orderSupplierManagementService.getInfoExport(orderSupplierManagementVo);
        ExcelUtil<OrderSupplierManagementExport> excelUtil = new ExcelUtil<OrderSupplierManagementExport>(OrderSupplierManagementExport.class);
        excelUtil.excelWriter(response, infoExport, "供应商信息信息.xlsx");
        return R.ok();
    }

    /**
     * 供应商信息导入
     */
    //@SaCheckPermission("order:purchase:supplier:import")
    @Log(title = "供应商信息导入", businessType = BusinessType.INSERT)
    @PostMapping("/supplierMessageImport")
    public R mouldMessageImport(MultipartFile file) throws IOException {
        ExcelUtil<OrderSupplierManagementimportExport> excelUtil = new ExcelUtil<OrderSupplierManagementimportExport>(OrderSupplierManagementimportExport.class);
        List<OrderSupplierManagementimportExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return R.ok(orderSupplierManagementService.importMouldData(coCustomer));
    }

    /**
     * 提交审核
     */

    //@SaCheckPermission("order:purchase:supplier:subExamine")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/reviewSubmit/{id}")
    public R<Void> reivewSubmit(@PathVariable("id") Long id) {
        return toAjax(orderSupplierManagementService.reivewSubmit(id));
    }

    /**
     * 供应商审核
     */
    //@SaCheckPermission("order:purchase:supplier:examine")
    @Log(title = "供应商审核", businessType = BusinessType.UPDATE)
    @PostMapping("/supplierReview")
    public R<Void> supplierReview(@Validated @RequestBody OrderReviewVo orderReviewVo) throws Exception {
        return toAjax(orderSupplierManagementService.supplierReview(orderReviewVo));
    }

    /**
     * 通过供应商名称获取供应商编码(财务模块使用)
     */
    @GetMapping("/getCodeBySupplierName")
    public String getCodeBySupplierName(String supplierName) {
        return orderSupplierManagementService.getCodeBySupplierName(supplierName);
    }

    /**
     * 供应商下拉
     */
    @GetMapping("/supplierPullDown")
    public List<PullDown> supplierPullDown() {
        return orderSupplierManagementService.supplierPullDown();
    }
}
